﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StoreHoursDomain.Entity;

namespace WebUI.Models
{
    public class StoreViewModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<WeeklyStoreHoursViewModel> AllHours { get; set; }
        public Dates Dates;

        public WeeklyStoreHoursViewModel CurrentHours
        {
            get
            {
                return AllHours.Where(hours => hours.EffectiveDate <= DateTime.Now).OrderByDescending(hours => hours.EffectiveDate).FirstOrDefault();
            }
        }

        public IEnumerable<WeeklyStoreHoursViewModel> PastHours
        {
            get
            {
                var currentHours = CurrentHours;
                return AllHours.Where(hours => hours.EffectiveDate < currentHours.EffectiveDate).OrderBy(hours => hours.EffectiveDate);
            }
        }

        public IEnumerable<WeeklyStoreHoursViewModel> FutureHours
        {
            get
            {
                var currentHours = CurrentHours;

                return AllHours.Where(hours => hours.EffectiveDate > currentHours.EffectiveDate).OrderBy(hours => hours.EffectiveDate);
            }
        }

        public static StoreViewModel GetStoreViewModel(Store store)
        {
            var storeViewModel = new StoreViewModel { ID = store.ID, Name = store.Name, AllHours = new List<WeeklyStoreHoursViewModel>() };
            storeViewModel.Dates = new Dates();

            foreach (var weeklyStoreHour in store.AllHours)
            {
                storeViewModel.AllHours.Add(WeeklyStoreHoursViewModel.GetWeeklyStoreHoursViewModel(weeklyStoreHour));
            }
            
            List<WeeklyStoreHoursViewModel> hours = new List<WeeklyStoreHoursViewModel>();
            hours.Add(storeViewModel.CurrentHours);
            hours.AddRange(storeViewModel.FutureHours);
            hours.AddRange(storeViewModel.PastHours);

            storeViewModel.AllHours = hours;

            return storeViewModel;
        }

    }
}